/**    
 * @{#} CreateQuestionSuccessAndChatActivity.java Create on 2014年10月17日 下午10:48:03    
 *    
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 * @description
 *	
 */
package com.smiier.skin;

import java.util.HashMap;
import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.o.app.ui.OImageView;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class CreateQuestionSuccessAndChatActivity extends BasicActivity {

	private OImageView mImgDoctorIcon;

	private TextView mTextOperateTip;

	private TextView mTextOperateTip1;

	private Button mBtnChat2Doctor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_question_successfully_chat);

		mImgDoctorIcon = (OImageView) findViewById(R.id.img_doctor_icon);
		mTextOperateTip = (TextView) findViewById(R.id.text_operate_tip);
		mTextOperateTip1 = (TextView) findViewById(R.id.text_operate_tip1);
		mBtnChat2Doctor = (Button) findViewById(R.id.btn_chat_2_doctor);

		init();

		setNavTitle("创建成功");
		setNavRightBtn("完成");
		back.setVisibility(View.GONE);

		try {
			JSONObject doctorInfo = new JSONObject(getIntent().getStringExtra(
					Constant.IDENTITY_KEY_DOCTOR_INFO));
			String iconPath = doctorInfo.getString("Pic");
			if (iconPath.indexOf(".") != -1) {
				iconPath = GlobalSettings.SERVER_IMG_URL + iconPath;
			} else {
				iconPath = GlobalSettings.SERVER_IMG_URL + iconPath
						+ Constant.IMG_TH;
			}
			// mBitmapUtils.display(mImgDoctorIcon, iconPath,
			// new OBitmapLoadCallBack<View>());
			CommonUtility.displayHeadImage(mImgDoctorIcon, mBitmapUtils,
					iconPath, getResources(), doctorInfo.getInt("Sex"));
			String doctorName = doctorInfo.getString("Name");
			mTextOperateTip.setText(String.format(Locale.getDefault(),
					CommonUtility.getText(mTextOperateTip), doctorName));
			mTextOperateTip1.setText(String.format(Locale.getDefault(),
					CommonUtility.getText(mTextOperateTip1), doctorName));
			mBtnChat2Doctor.setText(String.format(Locale.getDefault(),
					CommonUtility.getText(mBtnChat2Doctor), doctorName));
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.btn_chat_2_doctor) {
			String qid = getIntent().getStringExtra(Constant.IDENTITY_KEY);
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(Constant.PARAM_API, "Question.Get");
			params.put("qids", qid);
			RequestTaskIntercept.requestIntercept(activity,
					Constant.URL.REQUEST, new RequestHandler(activity,
							new HandlerCallback() {

								@Override
								public void callback(Object o) {
									if (!CommonUtility.isNull(o)) {
										try {
											JSONObject question = (JSONObject) o;
											question = question.getJSONArray(
													Constant.JSON_PARAM_RES)
													.getJSONObject(0);
											Intent freshSessionlistData = new Intent(
													Constant.RECIVER_CHAT_FINISH_REFRESH);
											freshSessionlistData.putExtra(
													Constant.IDENTITY_KEY,
													question.toString());
											activity.sendBroadcast(freshSessionlistData);

											Intent finishChatData = new Intent(
													Constant.RECEIVER_CHATING);
											finishChatData.putExtra(
													Constant.IDENTITY_KEY_1,
													true);
											activity.sendBroadcast(finishChatData);

											Intent intent = new Intent(
													activity,
													ChatActivity.class);
											intent.putExtra(
													Constant.IDENTITY_KEY,
													question.toString());
											startActivity(intent);
											finish();
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								}
							}), params);
		} else if (id == R.id.text_right) {
			finish();
		}
	}
	
	
	/*@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
